import Vue from 'vue'
import axios from "@/utils/axios.min.js";
import qs from "qs";
//const baseUrl="http://kf01.qg0517.xyz";
//const baseUrl="https://"+window.location.host;
let isFirstQrReq=true;

let vm = Vue.prototype;
console.log("vm:",vm);
// 创建axios实例
const httpService = axios.create({
    //API地址
    baseURL: 'https://kf01.qg0517.xyz',
    //baseURL: baseUrl,
    // 请求超时时间
    timeout: 30000,
    withCredentials: true
});

// // 单独另外的请求
// const writeService = axios.create({
//     //API地址
//     baseURL: 'http://open.devep.cn',
//     // 请求超时时间
//     timeout: 30000,
//     withCredentials: true
// });


// writeService.interceptors.request.use(
//     config => {
//         console.log("token:",window.localStorage.getItem("u_Token"));
//         if(window.localStorage.getItem("u_Token")!==null){
//           config.headers['token'] = window.localStorage.getItem("u_Token");
//         }


//         return config;
//     },
//     error => {
//         // 请求错误处理
//         Promise.reject(error);
//     }
// );


// request拦截器
// Cannot create property 'headers' on boolean 'false'
// https://blog.csdn.net/qq_31906983/article/details/88846570
httpService.interceptors.request.use(
    config => {
        let cur_version=window.localStorage.getItem("itemVersion");
        let cur_token=window.localStorage.getItem("token");
        if(cur_version!==null){
          config.headers['version'] = cur_version;
          if(cur_token!==null){
            config.headers['token'] = cur_token;
            // vm.$bus.$emit("toastEvent","已登录!");
            // setTimeout(()=>{
            //   // 在App.vue里定义
            //   goPage('/platform/home');
            // },2500);
          }else{
            //vm.$bus.$emit("toastEvent","请先登录!");
            setTimeout(()=>{
              // 在App.vue里定义
              goPage('/login');
            },2500);
          }
        }else{
          vm.$bus.$emit("toastEvent","版本过低，请更新版本!");
          setTimeout(()=>{
            // 在App.vue里定义
            goPage('/login');
          },2500);
        }
        console.log("config_0:",config);
        if(config.method!=='get'){
          let pwd={
            data:vm.$sec.encryption(config.data,'abcd6666efg89012')
          };
          //config.data=vm.$sec.encryption(config.data,'abcd6666efg89012');
          config.data=pwd;
          console.log("config_1:",config.data);
        }

        return config;
    },
    error => {
        // 请求错误处理
        Promise.reject(error);
    }
);

// response拦截器
httpService.interceptors.response.use(

    response => {
      console.log("response_0:",response);
        response.data=JSON.parse(vm.$sec.decrypt(response.data,'abcd6666efg89012'));
        console.log("response_1:",response.data);

      return response; // response一定要返回
    },
    // 处理处理
    error => {
      setTimeout(()=>{
        // 在App.vue里定义
        goPage('/login');
      },2500);
        //console.log("响应出错_00：",JSON.parse(vm.$sec.decrypt(error,'abcd6666efg89012')));
        // console.log("响应出错_01：",JSON.parse(vm.$sec.decrypt(error.response.data,'abcd6666efg89012')));
        // if(error.response.data.msg=="请登录后操作"){
        //   vm.$bus.$emit("toastEvent",error.response.data.msg);
        //   setTimeout(()=>{
        //     // 在App.vue里定义
        //     goPage('/login');
        //   },2500);
        // }

        return false;
    }
);


/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {},headObj={}) {

    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params,
            headers:headObj
        }).then(response => {
           resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {},headObj={}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: qs.stringify(params),
            headers:headObj
        }).then(response => {
             resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

// export function writePost(url, params = {},headObj={}) {
//     return new Promise((resolve, reject) => {
//         writeService({
//             url: url,
//             method: 'post',
//             data: qs.stringify(params),
//             headers:headObj
//         }).then(response => {
//              resolve(response);
//         }).catch(error => {
//             reject(error);
//         });
//     });
// }

export default {
    get,
    post,
    fileUpload
    //writePost
}
